package org.my.business.service.impl;

import lombok.RequiredArgsConstructor;
import org.my.business.domain.bo.DeviceBo;
import org.my.business.mapper.PcDeviceMapper;
import org.my.business.service.IDeviceService;
import org.my.common.core.domain.model.LoginUser;
import org.my.common.mybatis.core.page.PageQuery;
import org.my.business.domain.vo.DeviceVo;
import org.my.common.satoken.utils.LoginHelper;
import org.my.system.domain.SysUser;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@RequiredArgsConstructor
@Service
public class DeviceServiceImpl implements IDeviceService {

    private final PcDeviceMapper pcDeviceMapper;



    /**
     * 查询设备列表
     *
     * @param deviceBo
     * @param pageQuery
     * @return
     */
    @Override
    public List<DeviceVo> selectPageDeviceList(DeviceBo deviceBo, PageQuery pageQuery) {

        LoginUser loginUser = LoginHelper.getLoginUser();
        Long deptId = loginUser.getDeptId();
        deviceBo.setDeptId(deptId);

        return pcDeviceMapper.selectPageUserList(deviceBo);
    }

    /**
     * 机台运行状态数量统计
     * @param device
     * @return
     */
    @Override
    public HashMap<String,Object> staNum(DeviceBo device) {
        List<DeviceVo> devices= pcDeviceMapper.selectStaNum(device);

        int stop = 0;
        int start = 0;
        int online = 0;
        for (DeviceVo dev : devices) {
            String status = dev.getStatus();
            //在线机台才统计停止、运行状态
            if (status != null && dev.getIsOnline() == 2) {
                int num = Integer.valueOf(status, 16);
                //运行转态 0100  停止转态 0010 与运算后结果不变则确认状态
                if ((num & 4) == 4) {
                    start++;
                } else {
                    stop++;
                }
            }
            if (dev.getIsOnline() == 1) {
                online++;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("stop", stop);
        map.put("start", start);
        map.put("online", online);
        map.put("allNum", devices.size());
        return map;
    }

    /**
     * 员工、客户搜索下拉框内容
     * @param user
     * @return
     */
    @Override
    public List<HashMap<String, Object>> userList(SysUser user) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long deptId = loginUser.getDeptId();
        user.setDeptId(deptId);
        return pcDeviceMapper.selectQueryUsers(user);
    }

    /**
     * 单个设备详情
     * @param dev
     * @return
     */
    @Override
    public DeviceVo selectOneDev(DeviceBo dev) {
        return pcDeviceMapper.selectOneDev(dev.getDeviceId());
    }
}
